//
//  MXHomeFasionVC.m
//  MXの衣柜
//
//  Created by 马霄 on 15/8/10.
//  Copyright (c) 2015年 霄 马. All rights reserved.
// 首页潮品专区
#define WIDTH ([UIScreen mainScreen].bounds.size.width)
#define HEIGHT ([UIScreen mainScreen].bounds.size.height)
#import "MXHomeFasionVC.h"
#import "AFNetworkHandler.h"
#import "MXHomeFasionModel.h"
#import "MXHomeFashionTableViewCell.h"
//
#import "MXHomeFashionItemDetailsVC.h"
// 加入刷新的头文件
#import "MJRefresh.h"
#import "UIView+Extension.h"
#import "MBProgressHUD.h"
@interface MXHomeFasionVC ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
//-------------------- 设置接收ItemDetails 的全局数组 ----------------
@property (nonatomic, retain) NSMutableArray *ItemArray;
@property (nonatomic, retain) UIImageView *background;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, retain) MBProgressHUD *HUD;
@property (nonatomic, retain) UIView *alterView;
@end

@implementation MXHomeFasionVC
- (void)dealloc
{
    [_HUD release];
    [_alterView release];
    [_background release];
    [_ItemArray release];
    [_tableView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //全局数组初始化
    self.ItemArray = [NSMutableArray array];
    
    UILabel *titleLabel = [[[UILabel alloc] init] autorelease];
    titleLabel.width = WIDTHPRO * 100;
    titleLabel.height = HEIGHTPRO * 30;
    [titleLabel setTextColor:[UIColor blackColor]];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"潮品专区";
    self.navigationItem.titleView = titleLabel;
    
//    self.title = @"潮品专区";
    [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
    
    
    
    
    // 引入表视图
    [self creatTableView];
    // 引入数据
    [self GetData];
    
    [self uiActivityindicatorviewStart];

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(uiActivityindicatorviewEnd) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

    
    self.pageNum = 2;
    
    // 添加刷新方法
    [self loadingCollectionView];
    
    [self refreshTableView];
    
    [self creatBackGroundImage];
    
    [self.view insertSubview:self.tableView aboveSubview:self.background];
    
    // Do any additional setup after loading the view.
}
- (void)creatTableView
{
    self.tableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - HEIGHTPRO * 64) style:(UITableViewStylePlain)] autorelease];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    
}

-(void)uiActivityindicatorviewStart
{
    self.alterView =[[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)] autorelease];
    self.alterView.backgroundColor =[UIColor whiteColor];
    self.HUD = [MBProgressHUD showHUDAddedTo:self.alterView animated:YES];
//    self.HUD.labelText = @"正在努力的给您加载";
    [self.HUD show:YES];
    
    [self.view addSubview:self.alterView];
    
}
-(void)uiActivityindicatorviewEnd
{
    [self.alterView removeFromSuperview];
}


#pragma mark -- 下拉刷新
-(void)refreshTableView
{
    //    __block MXFindVC *findVC = self;
    [self.tableView addHeaderWithCallback:^{
        //        [self GetFindData];
        [self.tableView headerEndRefreshing];
    }];
    //添加下拉刷新头部控件
    
    //    [self.collectionView reloadData];
}

#pragma mark -- 上拉加载

-(void)loadingCollectionView
{
    
    //    __unsafe_unretained typeof(self) vc = self;
    [self.tableView addFooterWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSString *fasionpath = [NSString stringWithFormat:@"http://api.nanyibang.com/campaign?campaignType=chaopin&page=%ld", (long)(long)self.pageNum];
            self.pageNum += 1;
            [AFNetworkHandler GETwithAFNByURL:fasionpath completion:^(id result) {
                //        NSLog(@"%@", result);
                NSDictionary *dataDict = [result objectForKey:@"data"];
                
                NSArray *ItemDetailsArr = [dataDict objectForKey:@"itemDetail"];
                for (NSDictionary *dict in ItemDetailsArr) {
                    MXHomeFasionModel *model = [[[MXHomeFasionModel alloc] initWithDic:dict] autorelease];
                    [self.ItemArray addObject:model];
                }
                
                
            }];
            
            [self.tableView reloadData];
            [self.tableView footerEndRefreshing];
        });
    }];
}


/**************************** 产品部分 数据解析 ******************************/
- (void)GetData
{
    
    NSString *fasionpath = @"http://api.nanyibang.com/campaign?campaignType=chaopin&page=1";
    [AFNetworkHandler GETwithAFNByURL:fasionpath completion:^(id result) {
//        NSLog(@"%@", result);
        [self.ItemArray removeAllObjects];
        
        NSDictionary *dataDict = [result objectForKey:@"data"];
        
        NSArray *ItemDetailsArr = [dataDict objectForKey:@"itemDetail"];
        for (NSDictionary *dict in ItemDetailsArr) {
            MXHomeFasionModel *model = [[[MXHomeFasionModel alloc] initWithDic:dict] autorelease];
            [self.ItemArray addObject:model];
        }
        
        [self.tableView reloadData];
        [self uiActivityindicatorviewEnd];

    }];
}
/**************************** TableView 协议实现部分 ******************************/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// 根据解析出来的数据计算
    return self.ItemArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//根据自定义cell设定 应该是1个row
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 自定义cell
    MXHomeFashionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    if (cell == nil) {
        cell = [[MXHomeFashionTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"reuse"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell GetInfo:self.ItemArray[indexPath.section]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return HEIGHTPRO * 400;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MXHomeFasionModel *model = self.ItemArray[indexPath.section];
    MXHomeFashionItemDetailsVC *fashionDetailVC = [[[MXHomeFashionItemDetailsVC alloc] init] autorelease];
    fashionDetailVC.IDNum = model._id;
    [self presentViewController:fashionDetailVC animated:YES completion:^{
        NSLog(@"跳转详情页面");
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
}

#pragma mark - 开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    self.tabBarController.tabBar.hidden = YES;
}
#pragma mark - 结束减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.tabBarController.tabBar.hidden = NO;
}
- (void)creatBackGroundImage
{
    self.background = [[UIImageView alloc] initWithFrame:self.view.bounds];
    self.background.image = [UIImage imageNamed:@"Home_background"];
    [self.view addSubview:self.background];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
